﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;

namespace BookStore.admin
{
    public partial class Catogory : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll()
        {
            gvCatogory.DataSource = catogoryService.getAll();
            gvCatogory.DataBind();
        }
        public void ResetControl()
        {
            txtCatogoryID.Text ="";
            txtCatogoryname.Text = "";
        }

        public void insertCatogory()
        {
            catogoryInfo objcatogory = new catogoryInfo();

            if (catogoryService.checkExist(Convert.ToInt32(txtCatogoryID.Text)) == true)
            {
                Response.Write("<script language='javascript'>alert('catogoryID existed');</script>");
                return;
            }
            objcatogory.catogoryID = Convert.ToInt32(txtCatogoryID.Text);

            //if (catogoryService.checkExist(txtCatogoryID.Text)==true)
            //{
            //    Response.Write("<script language='javascript'>alert('catogoryID existed');</script>");
            //    return;
            //}
            //objcatogory.catogoryID = Convert.ToInt32(txtCatogoryID.Text);

            objcatogory.catogoryName = txtCatogoryname.Text;
            bool test = catogoryService.insertCatogory(objcatogory);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateCatogory()
        {
            catogoryInfo objcatogory = new catogoryInfo();

            objcatogory.catogoryID = Convert.ToInt32(txtCatogoryID.Text);

            objcatogory.catogoryID= Convert.ToInt32(txtCatogoryID.Text);

            objcatogory.catogoryName = txtCatogoryname.Text;
            bool test = catogoryService.updateCatogory(objcatogory);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                insertCatogory();
            }
            else
            {
                updateCatogory();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void gvCatogory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int CatogoryID = Convert.ToInt32(gvCatogory.Rows[e.RowIndex].Cells[0].Text);
            //if (catogoryService.checkCatogoryID(CatogoryID) == true)
            //{
            //    Response.Write("<script language='javascript'>alert('subcatogoy still existed , dont delete');</script>");
            //    return;
            //}

            bool test = catogoryService.deleteCatogory(CatogoryID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvCatogory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int CatogoryID = Convert.ToInt32(gvCatogory.Rows[e.NewSelectedIndex].Cells[0].Text);
            catogoryInfo objCatogory = new catogoryInfo();
            objCatogory.catogoryID = Convert.ToInt32(txtCatogoryID.Text);
            catogoryService.getbyID(Convert.ToInt32(txtCatogoryID.Text));
            txtCatogoryID.Text = objCatogory.catogoryID.ToString();
            txtCatogoryname.Text = objCatogory.catogoryName;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void gvCatogory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCatogory.PageIndex = e.NewPageIndex;
            showAll();
        }

       
    }
}
